use std::io;

// 数组类型
fn main() {
    // 一【声明和初始化数组】
    // let a = [1, 2, 3, 4, 5];        // 省略类型注解的写法
    // 分号写法①，声明时，指定元素类型和个数，形如：[元素类型; 元素个数]
    let a: [i32; 5] = [1, 2, 3, 4, 5]; // 带有类型注解的写法
    println!("a = {:?}", a);

    let months = [
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December",
    ];
    println!("months = {:?}", months);

    // 分号写法②,赋值时，为多个相同值初始化，形如：[初始值; 元素个数]
    let b = [3; 5];
    // 以上 等同于 let b = [3, 3, 3, 3, 3];
    println!("b = {:?}", b);

    // 二【访问数组】
    let first = a[0];
    let second = a[1];
    println!("first = {}, second = {}", first, second);

    // 三【访问无效数组】
    println!("请输入一个数组的索引：");
    let mut index = String::new();
    io::stdin()
        .read_line(&mut index)
        .expect("Failed to read line");
    let index: usize = index
        .trim()
        .parse()
        .expect("Index entered was not a number");
    let element = a[index];
    println!("The value of the element at index {index} is: {element}");
}
